c4c86b75bf643044db7e3c3575bcbf31adf8c4f9
4 * Created on Sep 25, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if( !defined('MEDIAWIKI') ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * A query action to return meta information about the wiki site.
36 class ApiQuerySiteinfo
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
:: __construct( $query, $moduleName, 'si' );
42 public function execute() {
43 $params = $this->extractRequestParams();
45 foreach( $params['prop'] as $p )
50 $fit = $this->appendGeneralInfo( $p );
53 $fit = $this->appendNamespaces( $p );
55 case 'namespacealiases':
56 $fit = $this->appendNamespaceAliases( $p );
58 case 'specialpagealiases':
59 $fit = $this->appendSpecialPageAliases( $p );
62 $fit = $this->appendMagicWords( $p );
65 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
66 $fit = $this->appendInterwikiMap( $p, $filteriw );
69 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
72 $fit = $this->appendStatistics( $p );
75 $fit = $this->appendUserGroups( $p );
78 $fit = $this->appendExtensions( $p );
80 case 'fileextensions':
81 $fit = $this->appendFileExtensions( $p );
84 $fit = $this->appendRightsInfo( $p );
87 $fit = $this->appendLanguages( $p );
90 ApiBase
:: dieDebug( __METHOD__
, "Unknown prop=$p" );
94 # Abuse siprop as a query-continue parameter
95 # and set it to all unprocessed props
96 $this->setContinueEnumParameter('prop', implode('|',
97 array_diff($params['prop'], $done)));
104 protected function appendGeneralInfo( $property ) {
109 $mainPage = Title
:: newFromText(wfMsgForContent('mainpage'));
110 $data['mainpage'] = $mainPage->getPrefixedText();
111 $data['base'] = $mainPage->getFullUrl();
112 $data['sitename'] = $GLOBALS['wgSitename'];
113 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
114 $data['phpversion'] = phpversion();
115 $data['phpsapi'] = php_sapi_name();
116 $data['dbtype'] = $GLOBALS['wgDBtype'];
117 $data['dbversion'] = $this->getDB()->getServerVersion();
119 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
123 // 'case-insensitive' option is reserved for future
124 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
126 if( isset( $GLOBALS['wgRightsCode'] ) )
127 $data['rightscode'] = $GLOBALS['wgRightsCode'];
128 $data['rights'] = $GLOBALS['wgRightsText'];
129 $data['lang'] = $GLOBALS['wgLanguageCode'];
130 if( $wgContLang->isRTL() )
132 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
135 $data['readonly'] = '';
136 $data['readonlyreason'] = wfReadOnlyReason();
138 if( $GLOBALS['wgEnableWriteAPI'] )
139 $data['writeapi'] = '';
141 $tz = $GLOBALS['wgLocaltimezone'];
142 $offset = $GLOBALS['wgLocalTZoffset'];
143 if( is_null( $tz ) ) {
146 } elseif( is_null( $offset ) ) {
149 $data['timezone'] = $tz;
150 $data['timeoffset'] = intval($offset);
151 $data['articlepath'] = $GLOBALS['wgArticlePath'];
152 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
153 $data['script'] = $GLOBALS['wgScript'];
154 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
155 $data['server'] = $GLOBALS['wgServer'];
156 $data['wikiid'] = wfWikiID();
158 return $this->getResult()->addValue( 'query', $property, $data );
161 protected function appendNamespaces( $property ) {
164 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
169 ApiResult
:: setContent( $data[$ns], $title );
170 $canonical = MWNamespace
::getCanonicalName( $ns );
172 if( MWNamespace
::hasSubpages( $ns ) )
173 $data[$ns]['subpages'] = '';
176 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
178 if( MWNamespace
::isContent( $ns ) )
179 $data[$ns]['content'] = '';
182 $this->getResult()->setIndexedTagName( $data, 'ns' );
183 return $this->getResult()->addValue( 'query', $property, $data );
186 protected function appendNamespaceAliases( $property ) {
187 global $wgNamespaceAliases, $wgContLang;
188 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
189 $namespaces = $wgContLang->getNamespaces();
191 foreach( $aliases as $title => $ns ) {
192 if( $namespaces[$ns] == $title ) {
193 // Don't list duplicates
199 ApiResult
:: setContent( $item, strtr( $title, '_', ' ' ) );
203 $this->getResult()->setIndexedTagName( $data, 'ns' );
204 return $this->getResult()->addValue( 'query', $property, $data );
207 protected function appendSpecialPageAliases( $property ) {
210 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
212 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
213 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
216 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
217 return $this->getResult()->addValue( 'query', $property, $data );
220 protected function appendMagicWords( $property ) {
223 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
225 $caseSensitive = array_shift($aliases);
226 $arr = array('name' => $magicword, 'aliases' => $aliases);
228 $arr['case-sensitive'] = '';
229 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
232 $this->getResult()->setIndexedTagName($data, 'magicword');
233 return $this->getResult()->addValue( 'query', $property, $data );
236 protected function appendInterwikiMap( $property, $filter ) {
237 $this->resetQueryParams();
238 $this->addTables( 'interwiki' );
239 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
241 if( $filter === 'local' )
242 $this->addWhere( 'iw_local = 1' );
243 elseif( $filter === '!local' )
244 $this->addWhere( 'iw_local = 0' );
246 ApiBase
:: dieDebug( __METHOD__
, "Unknown filter=$filter" );
248 $this->addOption( 'ORDER BY', 'iw_prefix' );
250 $db = $this->getDB();
251 $res = $this->select( __METHOD__
);
254 $langNames = Language
::getLanguageNames();
255 while( $row = $db->fetchObject($res) )
258 $val['prefix'] = $row->iw_prefix
;
259 if( $row->iw_local
== '1' )
261 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
262 if( isset( $langNames[$row->iw_prefix
] ) )
263 $val['language'] = $langNames[$row->iw_prefix
];
264 $val['url'] = $row->iw_url
;
268 $db->freeResult( $res );
270 $this->getResult()->setIndexedTagName( $data, 'iw' );
271 return $this->getResult()->addValue( 'query', $property, $data );
274 protected function appendDbReplLagInfo( $property, $includeAll ) {
275 global $wgShowHostnames;
278 if ( !$wgShowHostnames )
279 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
282 $lags = $lb->getLagTimes();
283 foreach( $lags as $i => $lag ) {
285 'host' => $lb->getServerName( $i ),
290 list( $host, $lag ) = wfGetLB()->getMaxLag();
292 'host' => $wgShowHostnames ?
$host : '',
293 'lag' => intval( $lag )
297 $result = $this->getResult();
298 $result->setIndexedTagName( $data, 'db' );
299 return $this->getResult()->addValue( 'query', $property, $data );
302 protected function appendStatistics( $property ) {
303 global $wgDisableCounters;
305 $data['pages'] = intval( SiteStats
::pages() );
306 $data['articles'] = intval( SiteStats
::articles() );
307 if ( !$wgDisableCounters ) {
308 $data['views'] = intval( SiteStats
::views() );
310 $data['edits'] = intval( SiteStats
::edits() );
311 $data['images'] = intval( SiteStats
::images() );
312 $data['users'] = intval( SiteStats
::users() );
313 $data['activeusers'] = intval( SiteStats
::activeUsers() );
314 $data['admins'] = intval( SiteStats
::numberingroup('sysop') );
315 $data['jobs'] = intval( SiteStats
::jobs() );
316 return $this->getResult()->addValue( 'query', $property, $data );
319 protected function appendUserGroups( $property ) {
320 global $wgGroupPermissions;
322 foreach( $wgGroupPermissions as $group => $permissions ) {
323 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
324 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
328 $this->getResult()->setIndexedTagName( $data, 'group' );
329 return $this->getResult()->addValue( 'query', $property, $data );
332 protected function appendFileExtensions( $property ) {
333 global $wgFileExtensions;
336 foreach( $wgFileExtensions as $ext ) {
337 $data[] = array( 'ext' => $ext );
339 $this->getResult()->setIndexedTagName( $data, 'fe' );
340 return $this->getResult()->addValue( 'query', $property, $data );
343 protected function appendExtensions( $property ) {
344 global $wgExtensionCredits;
346 foreach ( $wgExtensionCredits as $type => $extensions ) {
347 foreach ( $extensions as $ext ) {
349 $ret['type'] = $type;
350 if ( isset( $ext['name'] ) )
351 $ret['name'] = $ext['name'];
352 if ( isset( $ext['description'] ) )
353 $ret['description'] = $ext['description'];
354 if ( isset( $ext['descriptionmsg'] ) )
355 $ret['descriptionmsg'] = $ext['descriptionmsg'];
356 if ( isset( $ext['author'] ) ) {
357 $ret['author'] = is_array( $ext['author'] ) ?
358 implode( ', ', $ext['author' ] ) : $ext['author'];
360 if ( isset( $ext['version'] ) ) {
361 $ret['version'] = $ext['version'];
362 } elseif ( isset( $ext['svn-revision'] ) &&
363 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
364 $ext['svn-revision'], $m ) )
366 $ret['version'] = 'r' . $m[1];
372 $this->getResult()->setIndexedTagName( $data, 'ext' );
373 return $this->getResult()->addValue( 'query', $property, $data );
377 protected function appendRightsInfo( $property ) {
378 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
379 $title = Title
::newFromText( $wgRightsPage );
380 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
381 $text = $wgRightsText;
382 if( !$text && $title ) {
383 $text = $title->getPrefixedText();
387 'url' => $url ?
$url : '',
388 'text' => $text ?
$text : ''
391 return $this->getResult()->addValue( 'query', $property, $data );
394 public function appendLanguages( $property ) {
396 foreach( Language
::getLanguageNames() as $code => $name ) {
397 $lang = array( 'code' => $code );
398 ApiResult
::setContent( $lang, $name );
401 $this->getResult()->setIndexedTagName( $data, 'lang' );
402 return $this->getResult()->addValue( 'query', $property, $data );
406 public function getAllowedParams() {
409 ApiBase
:: PARAM_DFLT
=> 'general',
410 ApiBase
:: PARAM_ISMULTI
=> true,
411 ApiBase
:: PARAM_TYPE
=> array(
415 'specialpagealiases',
428 ApiBase
:: PARAM_TYPE
=> array(
433 'showalldb' => false,
437 public function getParamDescription() {
440 'Which sysinfo properties to get:',
441 ' general - Overall system information',
442 ' namespaces - List of registered namespaces and their canonical names',
443 ' namespacealiases - List of registered namespace aliases',
444 ' specialpagealiases - List of special page aliases',
445 ' magicwords - List of magic words and their aliases',
446 ' statistics - Returns site statistics',
447 ' interwikimap - Returns interwiki map (optionally filtered)',
448 ' dbrepllag - Returns database server with the highest replication lag',
449 ' usergroups - Returns user groups and the associated permissions',
450 ' extensions - Returns extensions installed on the wiki',
451 ' fileextensions - Returns list of file extensions allowed to be uploaded',
452 ' rightsinfo - Returns wiki rights (license) information if available',
453 ' languages - Returns a list of languages MediaWiki supports',
455 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
456 'showalldb' => 'List all database servers, not just the one lagging the most',
460 public function getDescription() {
461 return 'Return general information about the site.';
464 protected function getExamples() {
466 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
467 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
468 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
472 public function getVersion() {
473 return __CLASS__
. ': $Id$';